﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;
using QLCONGNOLIBS.Controllers.DungDD_Controller;
using QLCONGNOLIBS.Securities.DungDD_SecurityFramework;

namespace QLCONGNOAPPs.UsedForms.DungDD_UsedForm
{
    public partial class FrmLogin_Dung : Form
    {
        // datafield
        private SqlConnection mObjSQLConnection;
        private tblUser_SC userScEntity;
        public bool bClose = false;
        public FrmLogin_Dung(SqlConnection sqlconnect, tblUser_SC userEntity)
        {
            InitializeComponent();
            mObjSQLConnection = sqlconnect;
            userScEntity = userEntity;
            txt_TenTruyCap.Focus();
        }
        
        private void btn_DangNhap_Click(object sender, EventArgs e)
        {
            if (mObjSQLConnection.State == ConnectionState.Closed)
            {
                mObjSQLConnection = (new Commons.Services().getConnection());
            }
            LoginExecute();
        }

        public void LoginExecute()
        {
            errorProvider1.Clear();
            if (string.IsNullOrEmpty(txt_TenTruyCap.Text.Trim()))
            {
                errorProvider1.SetError(txt_TenTruyCap, "Bạn chưa nhập Tên truy cập");
                txt_TenTruyCap.Focus();
            }
            else if (string.IsNullOrEmpty(txt_MatKhau.Text.Trim()))
            {
                errorProvider1.SetError(txt_MatKhau, "Bạn chưa nhập Tên truy cập");
                txt_MatKhau.Focus();
            }
            else
            {
                try
                {
                    string strUserName = this.txt_TenTruyCap.Text.Trim();
                    string strPassword = this.txt_MatKhau.Text;
                    UserControllerNew objUserControllerNew = new UserControllerNew(mObjSQLConnection,userScEntity);
                    objUserControllerNew.CheckLogin(strUserName,strPassword);
                    if (userScEntity.IsLoginSuccess)
                    {
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("Thông tin tài khoản không đúng. Vui lòng nhập lại", "Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Error);
                        txt_TenTruyCap.Focus();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                finally
                {
                    mObjSQLConnection.Close();
                    
                }
            }
        }

    

        private void tabControl_DangNhap_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (Char)Keys.Enter)
            {
                LoginExecute();
            }
        }

        private void btn_HuyBo_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(@"Bạn muốn hủy bỏ đăng nhập?", "Hủy bỏ?", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
            {
                this.Close();
            }
            
        }
     
    }
}
